package cn.zhoutingrui.pethome.system.emp.service.impl;

import cn.zhoutingrui.pethome.base.service.impl.BaseServiceImpl;
import cn.zhoutingrui.pethome.system.emp.domain.EmpEchart;
import cn.zhoutingrui.pethome.system.emp.domain.Employee;
import cn.zhoutingrui.pethome.system.emp.domain.EmployeeBo;
import cn.zhoutingrui.pethome.system.emp.mapper.EmployeeMapper;
import cn.zhoutingrui.pethome.system.emp.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public List<Employee> findAll() {
        //查询所有员工数据
        List<EmployeeBo> empbos = employeeMapper.findAllBo();
        List<Employee> emps=new ArrayList<>();
        //把数据封装到bo
        for (EmployeeBo empbo : empbos) {
            Employee employee = new Employee();
            BeanUtils.copyProperties(empbo,employee);
            emps.add(employee);
        }
        return emps;
    }

    @Override
    public void add(Employee employee) {
        employeeMapper.add(employee);
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

    @Override
    public void delete(Long id) {
            Employee emp = employeeMapper.findById(id);
            if (emp.getState()==0){
                employeeMapper.delete(id);
            }else {
                throw new RuntimeException("当前员工已启用,无法删除");
            }
    }

    @Override
    public List<EmpEchart> findByEchart() {
        return employeeMapper.findByEchart();
    }
}
